//获取应用实例
const app = getApp()
Page({
  data: {
    item: 0,
    tab: 0,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    playlist: '',
    state: 'paused',
    playIndex: 0,
    play:{
      currentTime: '00:00',
      duration: '00:00',
      percent: 0,
      title: '',
      singer: '',
      coverImgUrl: '/images/你的答案.png',
    },
    playlist:[{
      id: 1, title: '你的答案', singer: '阿冗',
      src: 'http://music.163.com/song/media/outer/url?id=1400256289.mp3', coverImgUrl: '/images/你的答案.png'
    },{
      id: 2, title: '嚣张', singer: 'en',
      src: 'http://music.163.com/song/media/outer/url?id=1382596189.mp3', coverImgUrl: '/images/嚣张.png'
    },{
      id: 3, title: 'Faded', singer: 'Alan Walker',
      src: 'http://music.163.com/song/media/outer/url?id=36990266.mp3', coverImgUrl: '/images/Faded.png'
    },{
      id: 4, title: '摩天动物园', singer: 'G.E.M.邓紫棋',
      src: 'http://music.163.com/song/media/outer/url?id=1409382131.mp3', coverImgUrl: '/images/摩天动物园.png'
    },{
      id: 5, title: '绅士', singer: '薛之谦',
      src: 'http://music.163.com/song/media/outer/url?id=32192436.mp3', coverImgUrl: '/images/绅士.png'
    },{
      id: 6, title: '心如止水', singer: 'Yuess',
      src: 'http://music.163.com/song/media/outer/url?id=1363343839.mp3', coverImgUrl: '/images/心如止水.png'
    },{
      id: 7, title: '异类', singer: '华晨宇',
      src: 'http://music.163.com/song/media/outer/url?id=33190545.mp3', coverImgUrl: '/images/异类.png'
    },{
      id: 8, title: '遥远的她', singer: '张学友',
      src: 'http://music.163.com/song/media/outer/url?id=487154152.mp3', coverImgUrl: '/images/遥远的她.png'
    },{
      id: 9, title: '不将就', singer: '李荣浩',
      src: 'http://music.163.com/song/media/outer/url?id=31654343.mp3', coverImgUrl: '/images/不将就.png'
    },{
      id: 10, title: 'One Day', singer: 'Matisyahu',
      src: 'http://music.163.com/song/media/outer/url?id=21157332.mp3', coverImgUrl: '/images/One Day.png'
    }],
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  audioCtx: null,
  onReady: function(){
    this.audioCtx = wx.createInnerAudioContext()
    //默认选择第1曲
    this.setMusic(0)
  },
  setMusic: function(index){
    var music = this.data.playlist[index]
    this.audioCtx.src = music.src
    this.setData({
      playIndex: index,
      'play.title': music.title,
      'play.singer': music.singer,
      'play.coverImgUrl': music.coverImgUrl,
      'play.currentTime': '00:00',
      'play.duration': '00:00',
      'play.percent': 0
    })
  },
  //播放和暂停
  play: function(){
    this.audioCtx.play()
    this.setData({state: 'running'})
  },
  pause: function(){
    this.audioCtx.pause()
    this.setData({state: 'paused'})
  },
  //下一曲
  next: function(){
    var index = this.data.playIndex >= this.data.playlist.length - 1 ? 0 : this.data.playIndex + 1
    this.setMusic(index)
    if(this.data.state === 'running'){
      this.play()
    }
  },
  changeItem: function(e){
    this.setData({
      item: e.target.dataset.mi
    })
  },
  changeTab: function(e){
    this.setData({
      tab: e.detail.current
    })
  },
  // 进度和时间
  onReady: function(){
    this.audioCtx = wx.createInnerAudioContext()
    var that = this
    //播放失败检测
    this.audioCtx.onError(function(){
      console.log('播放失败: ' + that.audioCtx.src)
    })
    //播放完成自动换下一曲
    this.audioCtx.onEnded(function(){
      that.next()
    })
    //自动更新播放进度
    this.audioCtx.onPlay(function(){})
    this.audioCtx.onTimeUpdate(function(){
      that.setData({
        'play.duration': formatTime(that.audioCtx.duration),
        'play.currentTime': formatTime(that.audioCtx.currentTime),
        'play.percent': that.audioCtx.currentTime
      })
    })
    //默认选择第1曲
    this.setMusic(0)
    //格式化时间
    function formatTime(time){
      var minute = Math.floor(time / 60) % 60;
      var second = Math.floor(time) % 60
      return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
    }
  },
  sliderChange: function(e){
    var second = e.detail.value * this.audioCtx.duration / 100
    this.audioCtx.seek(second)
  },
  change: function(e){
    this.setMusic(e.currentTarget.dataset.index)
    this.play()
  }
})
